package com.acwing.lc.part1;

/**
 * @author rkc
 * @date 2022/9/12 15:10
 */
public class LC34在排序数组中查找元素的第一个和最后一个位置 {

    public int[] searchRange(int[] nums, int target) {
        if (nums.length == 0) return new int[]{-1, -1};
        int[] ans = new int[2];
        int l = 0, r = nums.length - 1;
        while (l < r) {
            int mid = l + r >> 1;
            if (nums[mid] >= target) r = mid;
            else l = mid + 1;
        }
        ans[0] = nums[l] == target ? l : -1;
        l = 0;
        r = nums.length - 1;
        while (l < r) {
            int mid = l + r + 1 >> 1;
            if (nums[mid] <= target) l = mid;
            else r = mid - 1;
        }
        ans[1] = nums[l] == target ? l : -1;
        return ans;
    }
}
